package com.example.healthmanagement.service;

import com.example.healthmanagement.dao.BmiRepository;
import com.example.healthmanagement.dao.ChildInformationUserRepository;
import com.example.healthmanagement.dao.SportRecordsRepository;
import com.example.healthmanagement.dao.SportRemRepository;
import com.example.healthmanagement.entity.*;
import com.example.healthmanagement.entity.vo.SportRemVo;
import com.example.healthmanagement.utils.JsonUtils;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class SportRemService {

    SportRemRepository sportRemRepository;
    BmiRepository bmiRepository;
    ChildInformationUserRepository childInfoRepository;

    SportRecordsRepository sportRecordsRepository;

    @Autowired
    public SportRemService(SportRemRepository sportRemRepository, BmiRepository bmiRepository, ChildInformationUserRepository childInfoRepository, SportRecordsRepository sportRecordsRepository) {
        this.sportRemRepository = sportRemRepository;
        this.bmiRepository = bmiRepository;
        this.childInfoRepository = childInfoRepository;
        this.sportRecordsRepository = sportRecordsRepository;
    }

    public boolean add(SportRem sportRem) {
        if (sportRemRepository.save(sportRem) != null) {
            return true;
        } else {
            return false;
        }
    }

    public boolean update(SportRem sportRem) {
        sportRemRepository.save(sportRem);
        return true;
    }

    public List<SportRemVo> findAll(String bookName) {
        List<SportRem> sportRems = new ArrayList<>();
        if (bookName != null) {
            sportRems = sportRemRepository.findSportRemsByDeletedAndAndSportNameContaining(false, bookName);
        } else {
            sportRems =  sportRemRepository.getSportRemsByDeleted(false);
        }
        List<SportRemVo> remVos = new ArrayList<>();
        for (SportRem sportRem : sportRems) {
            SportRemVo sportRemVo = new SportRemVo();
            sportRemVo.setId(sportRem.getId());
            sportRemVo.setSportName(sportRem.getSportName());
            sportRemVo.setSportContent(sportRem.getSportContent());
            sportRemVo.setCreatedAt(sportRem.getCreatedAt());
            sportRemVo.setDeleted(sportRem.getDeleted());
            sportRemVo.setBmiId(sportRem.getBmiId());
            Bmi bmi = bmiRepository.findBmiById(sportRem.getBmiId());
            sportRemVo.setBmiDesc(bmi.getDesc());
            remVos.add(sportRemVo);
        }
        return remVos;
    }

    public SportRem findSportRemById(int id) {
        return sportRemRepository.findSportRemByIdAndDeleted(id, false);
    }

    public SportRem getSportRemByChildId(int childId) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String today = format.format(new Date());
        List<ChildSportRecords> records = sportRecordsRepository.findAllByChildInformation_ChildIDAndCreateTimeContaining(childId, today);
        int totalEnergy = 0;
        for (ChildSportRecords record : records) {
            String sport = record.getSport();
            List<SportRecord> list = JsonUtils.toList(sport, SportRecord.class);
            for (SportRecord sportRecord : list) {
                totalEnergy += Integer.parseInt(sportRecord.getEnergy()) * sportRecord.getNumber();
            }
        }
        ChildInformation childInfo = childInfoRepository.getChildInformationByChildID(childId);
        String bmi = childInfo.getBmi();
        if (bmi == null || bmi.isEmpty()) {
            return null;
        }
        double bmiValue = Double.parseDouble(bmi);
        if (bmiValue < 18.5 && totalEnergy >= 500) {
            return null;
        } else if (bmiValue >= 18.5 && bmiValue < 24 && totalEnergy >= 800) {
            return null;
        } else if (bmiValue >= 24 && bmiValue < 28 && totalEnergy >= 1200) {
            return null;
        } else if (totalEnergy >= 1500){
            return null;
        }
        Bmi bmiObj = bmiRepository.findBmiByValue(bmiValue);
        List<SportRem> sportRems = sportRemRepository.findSportRemsByDeletedAndBmiId(false, bmiObj.getId());
        Random rand = new Random();
        int randomIndex = rand.nextInt(sportRems.size());
        SportRem sportRem = sportRems.get(randomIndex);
        if (sportRem != null) {
            return sportRem;
        }
        return new SportRem();
    }

    public SportRem getSportRemByBmi(double bmi) {
        Bmi bmiObj = bmiRepository.findBmiByValue(bmi);
        List<SportRem> sportRems = sportRemRepository.findSportRemsByDeletedAndBmiId(false, bmiObj.getId());
        Random rand = new Random();
        int randomIndex = rand.nextInt(sportRems.size());
        SportRem sportRem = sportRems.get(randomIndex);
        if (sportRem != null) {
            return sportRem;
        }
        return null;
    }
}
